package xyz.hubery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import xyz.hubery.enums.CodeEnum;
import xyz.hubery.mapper.BrandMapper;
import xyz.hubery.model.BusException;
import xyz.hubery.pojo.Brand;
import xyz.hubery.service.BrandService;

import java.util.List;

@Slf4j
@Transactional
@DubboService
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public Brand findById(Long id) {
        return brandMapper.selectById(id);
    }

    @Override
    public List<Brand> findAll() {
        return brandMapper.selectList(null);
    }

    @Override
    public void add(Brand brand) {
        brandMapper.insert(brand);
    }

    @Override
    public void update(Brand brand) {
        brandMapper.updateById(brand);
    }

    @Override
    public void delete(Long id) {
        brandMapper.deleteById(id);
    }

    @Override
    public Page<Brand> search(Brand brand, int page, int size) {
        return brandMapper.selectPage(Page.of(page,size),new LambdaQueryWrapper< Brand>()
                .like(brand != null &&  StringUtils.hasText(brand.getName()),Brand::getName,brand.getName()));
    }
}
